package com.imchen.cms.cmsweb.controller;

import com.imchen.cms.annotation.UserLoginToken;
import com.imchen.cms.exception.CmsException;
import com.imchen.cms.model.Investment;
import com.imchen.cms.service.InvestmentService;
import com.imchen.cms.utils.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;

/**
 * @author imchen
 * @create: 2019-01-15 14:01
 **/
@RestController
@RequestMapping(value = "/investment")
public class InvestmentController implements BaseController<Investment> {

    @Autowired
    private InvestmentService investmentService;

    @Override
    @PostMapping("/add")
    public HashMap add(@RequestBody Investment investment) throws CmsException {
        return investmentService.add(investment);
    }

    @Override
    @UserLoginToken
    @GetMapping("/remove/{id}")
    public int remove(@PathVariable int id) {
        return investmentService.removeById(id);
    }

    @Override
    @UserLoginToken
    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable int id) {
        return investmentService.deleteById(id);
    }

    @Override
    @UserLoginToken
    @PostMapping("/update")
    public int update(@RequestBody Investment investment) {
        return investmentService.update(investment);
    }

    @Override
    @UserLoginToken
    @GetMapping("/find")
    public HashMap<String, Object> find(int id) {
        return ServiceUtil.toObjectMap(investmentService.findById(id),"investment");
    }

    @UserLoginToken
    @GetMapping("/find/investments")
    public HashMap<String, Object> find(@RequestParam int index, @RequestParam int pageSize){
        return ServiceUtil.toObjectMap(investmentService.findByPage(index,pageSize),"investment");
    }

    @GetMapping("/find/pages")
    @UserLoginToken
    public HashMap<String, Object> findByPage(@RequestParam int index, @RequestParam int pageSize){
        return ServiceUtil.toObjectMap(investmentService.findByPageHelper(index,pageSize),"investment");
    }

    @GetMapping("/find/between")
    @UserLoginToken
    public HashMap<String, Object> find(@RequestParam String startDate, @RequestParam String endDate) throws ParseException {
        return ServiceUtil.toObjectMap(investmentService.findByDate(startDate,endDate),"comments");
    }

    @UserLoginToken
    @DeleteMapping("/restore/{id}")
    public int restore(@PathVariable int id) {
        return investmentService.restoreById(id);
    }
}